<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <title>Document</title>
    <style>
        .container>div{
            height: 300px;
        }
        .container>div:nth-child(1){
            background: red;
        }
        .container>div:nth-child(2){
            background: pink;
        }
        .container>div:nth-child(3){
            background: yellow;
        }
        .container>div:nth-child(4){
            background: green;
        }
        .nav{
            position: fixed;
            line-height: 50px;
            background: #eee;
            top:0;
        }
    </style>
</head>
<body>
    <div class="nav">
        <a href="#html">html</a>
        <a href="#css">css</a>
        <a href="#javascript">javascript</a>
        <a href="#vue">vue</a>

    </div>
    <div class="container">
        <div id="html">html</div>
        <div id="css">css</div>
        <div id="javascript">javascript</div>
        <div id="vue">vue</div>
    </div>
    <script>
        // obj.offset().top 获取元素距离顶部的距离
        // arrt() 获取元素的属性 
        // $('html,body').animate({scrollTop:params})
        $(".nav a").click(function(){
            // 1.获取当前元素对应跳转的元素距离顶部的高度
            var attr=$(this).attr("href");
            var top=$(attr).offset().top;
            console.log(attr);
            console.log(top);
            // 2.让窗口滚动对应的高度
            $("html,body").animate({scrollTop:top})
            return false;
        })
    
    </script>
</body>
</html>